import { ref } from 'vue'
import {ElLoading, ElMessage, ElMessageBox} from 'element-plus'
import { useRouter } from "vue-router";
export default function useTable({
    params,
    requestFn,
    size = 10,
    type='page',
    // 格式化
    format = (res) => res,
    routerPre
   }) {
    const router = useRouter()

    const searchParams = ref({
        ...params
    })
    const dataSource = ref([])
    const pageNo = ref(1)
    const pageSize = ref(size)
    const total = ref(0)
    const loading = ref(false)
    const reset = () => {
        searchParams.value = {
            ...params
        }
        search()
    }
    const changePage = (page) => {
        pageNo.value = page
        handlePage()
    }
    const changeSize = (size) => {
        pageSize.value = size
        handlePage()
    }

    const handlePage = () => {
        if (!requestFn) return
        loading.value = true
        // const loadingInstance = ElLoading.service()
        const params = {}
        if (type === 'page') {
            params.pageNum = pageNo.value
            params.pageSize = pageSize.value
        }

        requestFn[type]({
            ...searchParams.value,
            ...params
        }).then(res => {
            if (type === 'page') {
                dataSource.value = format(res.data.rows)
                total.value = res.data.count
            }
            if (type === 'list') {
                dataSource.value = format(res.data)
            }
        }).finally(() => {
            loading.value = false
            // loadingInstance.close()
        })
    }

    const search = () => {
        pageNo.value = 1
        handlePage()
    }

    const openAdd = () => {
        router.push({
            path: `/${routerPre}/save`
        })
    }
    const openView = (row) => {
        router.push({
            path: `/${routerPre}/detail/${row.id}`
        })
    }
    const openEdit = (row) => {
        router.push({
            path: `/${routerPre}/save/${row.id}`
        })
    }
    const openCopy = (row) => {
        router.push({
            path: `/${routerPre}/save`,
            query: {
                copyId: row.id
            }
        })
    }
    const openDelete = (row) => {
        ElMessageBox.confirm(
            '是否确认删除？',
            '提示',
            {
                confirmButtonText: '确认删除',
                cancelButtonText: '取消',
                type: 'error',
            }
        )
            .then(() => {
                const loadingInstance = ElLoading.service()
                requestFn.del(row.id).then(res => {
                    ElMessage({
                        type: 'success',
                        message: '删除成功',
                    })
                    search()
                }).finally(() => {
                    loadingInstance.close()
                })
            })
            .catch((e) => {
                console.log(e)
                ElMessage({
                    type: 'info',
                    message: '删除取消',
                })
            })
    }

    const updateStatus = (row) => {
        requestFn.update({
            id: row.id,
            status: row.status
        }).then(res => {
            ElMessage({
                type: 'success',
                message: '操作成功',
            })
            search()
        })
    }
    search()
    return {
        searchParams,
        total,
        pageNo,
        pageSize,
        dataSource,
        changePage,
        changeSize,
        search,
        reset,
        openAdd,
        openView,
        openEdit,
        openCopy,
        openDelete,
        updateStatus
    }
}